package com.szbber.medical.bed.ui.fragment

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.gson.Gson
import com.orhanobut.logger.Logger
import com.szbber.commen.base.BaseVMFragment
import com.szbber.commen.bean.BedCardInfoBean
import com.szbber.commen.bean.MessageListBean
import com.szbber.commen.bean.NurseLevelListBean
import com.szbber.commen.dialog.DialogUtils
import com.szbber.commen.utils.QRcode
import com.szbber.medical.bed.R
import com.szbber.medical.bed.databinding.BedFragmentPatientInfoBinding
import com.szbber.medical.bed.databinding.BedItemBedcarelabelBinding
import com.szbber.medical.bed.databinding.BedItemMessageListBinding
import com.szbber.medical.bed.ui.activity.LockScreenActivity
import com.szbber.medical.bed.viewmodel.fragment.PatientInfoViewModel
import kotlinx.android.synthetic.main.bed_fragment_patient_info.*

class PatientInfoFragment : BaseVMFragment<BedFragmentPatientInfoBinding, PatientInfoViewModel>(),
    View.OnClickListener {

    companion object {
        fun getInstance(): PatientInfoFragment {
            return PatientInfoFragment()
        }
    }

    private var patientVo:BedCardInfoBean.PatientVo? = null

    override fun getLayoutResId(): Int = R.layout.bed_fragment_patient_info

    override fun initVM(): PatientInfoViewModel = ViewModelProvider(this,
        ViewModelProvider.NewInstanceFactory()).get(PatientInfoViewModel::class.java)

    override fun initView(savedInstanceState: Bundle?) {
        cvMoom.setOnClickListener(this)
        llRefresh.setOnClickListener(this)
        mBinding.patientInfoVM = mViewModel
        mViewModel.getBedCardInfo(activity)
        observeNurseLevel()
        observeMessageList()
    }

    private fun observeNurseLevel() {
        mViewModel.bedCardInfoBean.observe(this) {
             patientVo = it.data.patientVo
            try {
                val strQRcode = patientVo?.name + patientVo?.age + patientVo?.cure_no + patientVo?.bed_code
                QRcode().generateQRCode(strQRcode, ivQRcode)?.let {
                    ivQRcode.setImageBitmap(it)
                }
            }catch (e :Exception){
                e.printStackTrace()
            }
            it.data.nurseLevel?.font_color?.let{
                tvLevel.setTextColor(Color.parseColor(it))
            }
            it.data.nurseLevel?.bg_color?.let{
                tvLevel.setBackgroundColor(Color.parseColor(it))
            }

            val adapter = object :
                BaseQuickAdapter<BedCardInfoBean.BedSideCareLabelVo, BaseViewHolder>(R.layout.bed_item_bedcarelabel,
                    it.data.bedSideCareLabelVo.toMutableList()) {
                override fun convert(holder: BaseViewHolder,
                    item: BedCardInfoBean.BedSideCareLabelVo) {
                    val binding =
                        DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as BedItemBedcarelabelBinding
                    binding.item = item
                    binding.rlLabel.setBackgroundColor(Color.parseColor(item.bg_color))
                    binding.tvName.setTextColor(Color.parseColor(item.font_color))
                    binding.executePendingBindings()
                }
            }
            val manager = LinearLayoutManager(activity)
            mRecyclerView.adapter = adapter
            mRecyclerView.layoutManager = manager
        }
    }

    private fun observeMessageList() {
        mViewModel.messageListBean.observe(this) {
            val adapter = object :
                BaseQuickAdapter<MessageListBean.Data, BaseViewHolder>(R.layout.bed_item_message_list,
                    it.data.toMutableList()) {
                override fun convert(holder: BaseViewHolder, item: MessageListBean.Data) {
                    val binding =
                        DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as BedItemMessageListBinding
                    binding.messageVM = item
                    binding.tvSure.setOnClickListener {
                        DialogUtils.showMessageDialog(activity, item)
                    }
                }
            }
            rvMessage.adapter = adapter
            val manager = LinearLayoutManager(activity)
            rvMessage.layoutManager = manager
        }
    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.cvMoom->{
                val intent = Intent(activity,LockScreenActivity::class.java)
                intent.putExtra("data", Gson().toJson(patientVo))
                startActivity(intent)
            }
            R.id.llRefresh->{
                mViewModel.getBedCardInfo(activity)
            }
        }
    }


}